package com.uzhie.jt808.service.impl;

import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.msg.JT808MsgBean;
import com.uzhie.jt808.service.BaseService;
import com.uzhie.jt808.service.JT808MediaService;
import com.uzhie.jt808.spring.MsgIDMapping;
import com.uzhie.jt808.spring.MsgService;
import com.uzhie.jt808.task.Task0801;
import com.uzhie.jt808.task.Task1205;
import com.uzhie.jt808.util.JT808ProtocolUtil;
import com.uzhie.web.service.listener.MQSendService;
import io.netty.buffer.ByteBufUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.nio.ByteBuffer;

/**
 * Created by TangFei on 2018/8/9.
 * 多媒体协议
 */
@MsgService(name = "media")
public class JT808MediaServiceImpl extends BaseService implements JT808MediaService {

    @Autowired
    @Qualifier(value = "ftpSendService")
    private MQSendService ftpSendService;

    @Autowired
    @Qualifier(value = "passengerSendService")
    private MQSendService passengerSendService;

    @Autowired
    @Qualifier(value = "pictureSendService")
    private MQSendService pictureSendService;

    @Autowired
    @Qualifier(value = "originSendService")
    private MQSendService originSendService;

    @Autowired
    @Qualifier(value = "jt808ThreadExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    /**
     * 多媒体事件信息上传 0X0800
     *
     * @param msgBean
     * @return
     */
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_MEDIA_EVENT)
    public JT808MsgBean eventUpload(JT808MsgBean msgBean) {
        byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
        return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_COMMON, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
    }

    /**
     * 终端多媒体目录上传
     *
     * @param msgBean
     * @return
     */
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_PLAYBACK_CATALOG)
    public JT808MsgBean devUploadMediaIndex(JT808MsgBean msgBean) {
        taskExecutor.execute(new Task1205(msgBean, originSendService, sysCache));
        // 回复通用应答
        byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
        return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_COMMON, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
    }


    /**
     * 多媒体数据上传 0X0801
     *
     * @param msgBean
     * @return
     */
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_MEDIA_DATA)
    public JT808MsgBean dataUpload(JT808MsgBean msgBean) {
        logger.debug("收到的多媒体上传消息体数据为：" + ByteBufUtil.hexDump(msgBean.getBodyBytes()));
        // 异步写入图片
        taskExecutor.execute(new Task0801(msgBean, pictureSendService));
        //TODO 返回 0x8800 应答 暂时只返回多媒体ID
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        byteBuffer.put(msgBean.getBodyBytes(), 0, 4);
        return super.build808Obj(JT808Constant.MSG_ID.PLA_RES_MEDIA_DATA, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), byteBuffer.array());
    }


    /**
     * 摄像头立即拍摄
     *
     * @param msgBean
     * @return
     */
    public JT808MsgBean cameraTake(JT808MsgBean msgBean) {

        return null;
    }

    /**
     * 录音开始
     *
     * @param msgBean
     * @return
     */
    public JT808MsgBean soundRecordStart(JT808MsgBean msgBean) {

        return null;
    }

    /**
     * 检索终端存储多媒体数据和提取
     *
     * @param msgBean
     * @return
     */
    public JT808MsgBean queryMediaData(JT808MsgBean msgBean) {

        return null;
    }


    /**
     * 客流量信息上传
     *
     * @param msgBean
     * @return
     */
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_PASSENGER)
    public JT808MsgBean upPassenger(JT808MsgBean msgBean) {
        logger.debug("收到客流量信息上传数据为：" + ByteBufUtil.hexDump(msgBean.getBodyBytes()));
        passengerSendService.sendMessage(JT808Constant.MSG_ID.DEV_REQ_PASSENGER, msgBean.getHeader().getSimNum(), msgBean.getBodyBytes());
        byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
        return super.build808Obj(JT808Constant.MSG_ID.DEV_REQ_PASSENGER, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
    }

    /**
     * 文件上传完成通知
     *
     * @param msgBean
     * @return
     */
    @MsgIDMapping(msgID = JT808Constant.MSG_ID.DEV_REQ_FILE_UPLOAD_COMPLETE)
    @Override
    public JT808MsgBean upFileComplete(JT808MsgBean msgBean) {
        byte[] body = msgBean.getBodyBytes();
        int flowNum = JT808ProtocolUtil.parseWord2Int(body, 0);
        int result = JT808ProtocolUtil.parseByte2Int(body, 2);
        byte[] cacheVal = (byte[]) sysCache.getCacheVal(flowNum);
        ftpSendService.sendMessage(JT808Constant.MSG_ID.DEV_REQ_FILE_UPLOAD_COMPLETE, msgBean.getHeader().getSimNum(), cacheVal);
        if (0 == result) {
            byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 0);
            return super.build808Obj(JT808Constant.MSG_ID.DEV_REQ_PASSENGER, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
        } else {
            byte[] bodyBytes = super.commonRespBodyBytes(msgBean.getHeader().getFlowNum(), msgBean.getHeader().getMsgID(), 1);
            return super.build808Obj(JT808Constant.MSG_ID.DEV_REQ_PASSENGER, sysCache.getPlaFlowNum(), msgBean.getHeader().getSimNum(), bodyBytes);
        }
    }
}
